package creator;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class DaoCreator {
	
	public static void createFile(String dir, ObjectInfo o) {
		createDaoJavaFile(dir, o);
		createDaoImplJavaFile(dir, o);
	}
	
	public static void createDaoJavaFile(String dir, ObjectInfo o) {
		String idaoName = o.idaoName();
		String fileName = String.format("%s/dao/%s.java", dir, idaoName);
		
		try {
			FileCreator.createFile(fileName);
			FileWriter fw = new FileWriter(fileName);
			String packageName = o.getPackageName();
			
			fw.write(String.format("package %s.dao;\n\n", packageName));
			
			fw.write(String.format("import %s;\n\n", o.fullName()));
			
			fw.write(String.format("public interface %s {\n\n", idaoName));
			
			fw.write("}\n");
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void createDaoImplJavaFile(String dir, ObjectInfo o) {
		String daoName = o.daoName();
		String fileName = String.format("%s/dao/impl/%s.java", dir, daoName);
		
		try {
			FileCreator.createFile(fileName);
			FileWriter fw = new FileWriter(fileName);
			String packageName = o.getPackageName();
			
			fw.write(String.format("package %s.dao.impl;\n\n", packageName));
			
			fw.write(String.format("import %s;\n", o.fullName()));
			fw.write(String.format("import %s;\n\n", o.idaoFullName()));
			
			fw.write(String.format("public class %s implements %s {\n\n", daoName, o.idaoName()));
			
			fw.write("}\n");
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
